// TypeScript file
class HomeUI extends eui.Component
{
    constructor() 
    {
        super();

        console.log("new HomeUI");
        this.addEventListener( eui.UIEvent.COMPLETE, this.uiCompHandler, this );
        this.skinName = homeUISkin;
    }

    public pageReadyHandler(name: string): void
    {
        console.log("页面就绪：", name);
        for (var i: number = this.btns.length - 1; i > -1; --i)
        {
            this.btns[i].enabled = !this.btns[i].selected;
        }

        switch (name)
        {
            case GamePages.PROFILE:
            {
                if (!this.profileUI)
                {
                    this.profileUI = new ProfileUI();
                    this.profileUI.addEventListener(GameEvents.EVT_RETURN, this.onReturn, this);
                }

                this.imgBg.source = "commonBg_jpg";
                this.uiFocused = this.profileUI;
            }
            break;
            case GamePages.HEROS:
            {
                if (!this.herosUI)
                {
                    this.herosUI = new HerosUI();
                    this.herosUI.addEventListener(GameEvents.EVT_RETURN, this.onReturn, this);
                }

                this.imgBg.source = "bgListPage_jpg";
                this.uiFocused = this.herosUI;
            }
            break;
            case GamePages.GOODS:
            {
                if (!this.goodsUI)
                {
                    this.goodsUI = new GoodsUI;
                    this.goodsUI.addEventListener(GameEvents.EVT_RETURN, this.onReturn, this);
                }
                this.imgBg.source = "bgListPage_jpg";
                this.uiFocused = this.goodsUI;
            }
            break;
            case GamePages.ABOUT:
            {
                if (!this.aboutUI)
                {
                    this.aboutUI = new AboutUI();
                    this.aboutUI.addEventListener(GameEvents.EVT_CLOSE_ABOUT, this.onCloseAbout, this);
                }
                this.uiFocused = this.aboutUI;
            }
            break;
        }

        this.addChildAt(this.uiFocused, this.getChildIndex(this.imgBg) + 1);
    }

    private onCloseAbout(): void
    {
        this.resetFocus();
        let mbtn: eui.ToggleButton;
        switch (this.pageFocusePrev)
        {
            case GamePages.PROFILE:
            {
                mbtn = this.mbtnProfile;
                //this.mbtnProfile.selected = true;
                //this.mbtnProfile.dispatchEventWith(egret.TouchEvent.TOUCH_TAP);
            }
            break;
            case GamePages.HEROS:
            {
                mbtn = this.mbtnHeros;    
            }
            break;
            case GamePages.GOODS:
            {
                mbtn = this.mbtnGoods;
            }
            break;
        }
        if (mbtn)
        {
            mbtn.selected = true;
            mbtn.dispatchEventWith(egret.TouchEvent.TOUCH_TAP);
        }
        
    }

    private onReturn(): void
    {
        this.resetFocus();
        this.goHome();
    }
    private resetFocus(): void
    {
        if (this.uiFocused)
        {
            if (this.uiFocused.parent)
            {
                this.uiFocused.parent.removeChild(this.uiFocused);
            }
            this.uiFocused = null;
        }

        if (this.mbtnFocused)
        {
            this.mbtnFocused.selected = false;
            this.mbtnFocused.enabled = true;
            this.mbtnFocused = null;
        }
    }
    private uiCompHandler( evt : eui.UIEvent ) : void
    {
        console.log( "HomeUI uiCompHandler" );
        this.mbtnProfile.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.mbtnHeros.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.mbtnGoods.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.mbtnAbout.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );

        this.btns = 
        [
            this.mbtnProfile,
            this.mbtnHeros,
            this.mbtnGoods,
            this.mbtnAbout
        ];

        this.goHome();
    }

    private goHome() : void
    {
       console.log( "-------------------------HOME--------------------");
       this.pageFocusePrev = this.pageFocused = "home"; 
       this.imgBg.source = "homeBg_jpg";
    }
    private mbtnHandler( evt : egret.TouchEvent ) : void
    {
        if (evt.currentTarget == this.mbtnFocused)
        {
            return;
        }

        for (let i: number = 0; i < this.btns.length; ++i)
        {
            this.btns[i].enabled = false;
        }

        if (this.mbtnFocused)
        {
            this.mbtnFocused.selected = false;
            this.mbtnFocused.enabled = true;
        }

        if (this.uiFocused && this.uiFocused.parent)
        {
            this.uiFocused.parent.removeChild(this.uiFocused);
        }
        this.mbtnFocused = evt.currentTarget;
        console.log("选中", this.mbtnFocused.name);
        this.uiFocused = null;

        this.pageFocusePrev = this.pageFocused;

        switch (this.mbtnFocused)
        {
            case this.mbtnProfile:
            {
                this.pageFocused = GamePages.PROFILE;
            }
            break;
            case this.mbtnHeros:
            {
                this.pageFocused = GamePages.HEROS;
            }
            break;
            case this.mbtnGoods:
            {
                this.pageFocused = GamePages.GOODS;
            }
            break;
            case this.mbtnAbout:
            {
                this.pageFocused = GamePages.ABOUT;
            }
            break;
        }

        this.dispatchEventWith(GameEvents.EVT_LOAD_PAGE, false, this.pageFocused);

    }

    private mbtnProfile : eui.ToggleButton;
    private mbtnHeros : eui.ToggleButton;
    private mbtnGoods : eui.ToggleButton;
    private mbtnAbout : eui.ToggleButton;
    private mbtnFocused : eui.ToggleButton;

    private profileUI : ProfileUI;
    private herosUI : HerosUI;
    private goodsUI : GoodsUI;
    private aboutUI : AboutUI;
    private uiFocused : eui.Component;

    private imgBg : eui.Image;
    private pageFocused : string;
    private pageFocusePrev : string;

    private btns : eui.ToggleButton[];
}